//#define __DEBUG__
// by Xeno
#define THIS_FILE "fn_respawneh.sqf"
#include "xr_macros.sqf"

private ["_old", "_d_pos"];
_old = _this select 1;
__TRACE_1("","_old");
_mpos = markerPos QGVARXR(resp_marker);
_mpos set [2,0];
__TRACE_1("","_mpos");
player setPos _mpos;
if (__pGetVar(GVARXR(isdead))) exitWith {};
__TRACE("playActionNow Die");
player playActionNow "Die"; // takes loooong
__pSetVar [QGVARXR(pluncon), true, true]; // just to be sure
_norm_resp = false;
if (count __pGetVar(GVARXR(death_pos)) == 0) then {
	_norm_resp = true;
	_old call FUNCXR(CheckRespawn);
};
deleteVehicle _old;
__TRACE_1("","_norm_resp");
[QGVARXR(setCap), [player,true]] call d_fnc_NetCallEvent;
_d_pos = __pGetVar(GVARXR(death_pos));
__TRACE_1("","_d_pos");
if (count _d_pos > 0) then {
	__TRACE("pos to old pos and dir");
	player setDir (_d_pos select 1);
	player setPos (_d_pos select 0);
	if (!_norm_resp) then {
		[1] spawn FUNCXR(uncon);
		if (GVARXR(with_marker)) then {
			[QGVARXR(umarker), player] call d_fnc_NetCallEvent;
		};
	} else {
		__TRACE("spawning go uncon");
		0 spawn {
			if (surfaceIsWater (getPosASL player)) then {
				__TRACE("respawneh spawn watferfix check start");
				private "_shandle";
				_shandle = 0 spawn FUNCXR(waterfix);
				waitUntil {scriptDone _shandle};
				__TRACE("respawneh spawn watferfix done");
			};
			private ["_pos", "_slope"];
			waitUntil {speed player < 0.5};
			_pos = position player;
			_slope = [_pos, 1] call FUNCXR(GetSlope);
			__TRACE_2("respawneh spawn","_pos","_slope");
			if (_slope >= 0.78) then {
				__TRACE("respawneh spawn in slope, new position");
				[_pos, _slope, player] call FUNCXR(DoSlope);
			};
			[0] spawn FUNCXR(uncon);
		};
	};
};
if (d_WithAcre) then {
	[true] call acre_api_fnc_setSpectator;
};
__pSetVar [QGVARXR(pisinaction), false];
__pSetVar [QGVARXR(is_dragging), false];
__pSetVar [QGVARXR(dragged), false, true];
__pSetVar [QGVARXR(busyt), -1, true];
if (rating player < 0) then {
	player addRating abs(rating player);
};
0 spawn {
	_etime = time + 5;
	waitUntil {BFUNC(feedback_allowPP) || {time > _etime}};
	BFUNC(feedback_allowPP) = false;
};
player setDamage 0;
BFUNC(feedback_burningTimer) = 0;
player setFatigue 0;